<?php

namespace app\shopapi\controller;


use app\api\model\shop\ShopFans;
use app\common\controller\Shopapi;
use app\common\library\Ems;
use app\common\library\Sms;
use app\shopapi\model\ShopUser;
use fast\Random;
use think\Config;
use think\Db;
use think\Exception;
use think\Validate;

/**
 * 会员提现接口
 */
class Withdraw extends Shopapi
{

    protected $noNeedRight = '*';



    /**
     * 余额提现
     * @return string
     */
    public function withdraw()
    {
        $config = \app\admin\model\shop\Config::where(['id'=>1])
            ->field('withdrawtips,minmoney,maxmoney')->find();
        $this->success('',$config);
    }

    /**
     * 余额日志
     * @return string
     */
    public function withdrawlog()
    {
        $start_time = input('date');
        $start_time = empty($start_time) ? strtotime(date('Y-m', strtotime("today"))) : strtotime($start_time);
        $end_time = strtotime("+1 month", $start_time);

        $withdrawloglist = \app\admin\model\shop\Withdraw::where(['shop_id' => $this->shopauth->shop_id])
            ->where('createtime', 'between time', [$start_time, $end_time])
            ->order('id desc')
            ->paginate(10);
        $this->success('',$withdrawloglist);
    }

    /**
     * 创建订单并发起支付请求
     * @throws \think\exception\DbException
     */
    public function submit()
    {
        $money = $this->request->request('money');
        //$account = $this->request->request('account');
        //$name = $this->request->request('name');
        $type = 'wechat';
        $shop = \app\api\model\shop\Shop::get($this->shopauth->shop_id);

        //刷新Token
        $this->request->token();

        if ($money <= 0) {
            $this->error('提现金额不正确');
        }
        if ($money > $shop['money']) {
            $this->error('提现金额超出可提现金额');
        }
        /* if (!$account) {
             $this->error("提现账户不能为空");
         }
         if (!$name) {
             $this->error("真实姓名不能为空");
         }
         if (!Validate::is($account, "email") && !Validate::is($account, "/^1\d{10}$/")) {
             $this->error("提现账户只能是手机号或Email");
         }*/

        $config = \app\admin\model\shop\Config::where(['id'=>1])
            ->field('withdrawtips,minmoney,maxmoney')->find();

        if (isset($config['minmoney']) && $money < $config['minmoney']) {
            $this->error('提现金额不能低于' . $config['minmoney'] . '元');
        }
        if (isset($config['maxmoney']) && $money > $config['maxmoney']) {
            $this->error('提现金额不能高于' . $config['maxmoney'] . '元');
        }

        Db::startTrans();
        try {
            $data = [
                'orderid' => date("YmdHis") . sprintf("%08d", $this->shopauth->id) . mt_rand(1000, 9999),
                'shop_user_id' => $this->shopauth->id,
                'shop_id' => $this->shopauth->shop_id,
                'money'   => $money,
                'type'    => $type,
                //'account' => $account,
                //'name'    => $name,
            ];
            \app\admin\model\shop\Withdraw::create($data);
            \app\shopapi\model\Shop::money(-$money, $this->shopauth->shop_id, "提现");

            Db::commit();
        } catch (Exception $e) {
            Db::rollback();
            $this->error($e->getMessage());
        }
        $this->success("提现申请成功！请等待后台审核！");
        return;
    }
}